﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

using Microsoft.Practices.Unity;

using Happy.Unity;

namespace Happy.Bootstrap.Unity.Internal
{
    internal class UnitOfWorkRegister : IConventionRegister
    {
        public bool Register(IUnityContainer container, Type serviceType)
        {
            if (serviceType.Name.EndsWith("UnitOfWork"))
            {
                if (HttpContext.Current == null)
                {
                    container.RegisterTypeForAllInterface(serviceType, () => new PerThreadLifetimeManager());
                }
                else
                {
                    container.RegisterTypeForAllInterface(serviceType, () => new PerRequestLifetimeManager());
                }

                return true;
            }

            return false;
        }
    }
}
